﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using UIWindows.Models.ParameterModels;
using UIWindows.Tools.Converter;
using XingLucifer.IBase.Enums;

namespace UIWindows.GlobalStatics
{

    [Attributes.Singleton]
    public class DeviceInteractiveaddressStatic
    {
        public Dictionary<MachineInfoTypes,ObservableCollection<BaseParameteModel>> BaseParameters { get; set; } = new Dictionary<MachineInfoTypes, ObservableCollection<BaseParameteModel>>();
        public Dictionary<MachineInfoTypes, ObservableCollection< CollectParameterModel>> CollectParameters { get; set; } = new Dictionary<MachineInfoTypes, ObservableCollection<CollectParameterModel>>();
        public Dictionary<MachineInfoTypes, ObservableCollection<MesOutputParameterModel>>  MesOutputParameters { get; set; } = new Dictionary<MachineInfoTypes, ObservableCollection<MesOutputParameterModel>>();
        public Dictionary<(MachineInfoTypes, ShiftTypes), ObservableCollection<MesRunParameterModel>> MesRunParameters { get; set; } = new Dictionary<(MachineInfoTypes, ShiftTypes), ObservableCollection<MesRunParameterModel>>();

        public DeviceInteractiveaddressStatic()
        {
            if (File.Exists(GlobalStatic.DeviceInteractiveaddressPath))
            {
                try
                {
                    //var model = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(File.ReadAllText(GlobalStatic.DeviceInteractiveaddressPath, System.Text.Encoding.UTF8));
                    //BaseParameters = System.Text.Json.JsonSerializer.Deserialize<Dictionary<MachineInfoTypes, ObservableCollection<BaseParameteModel>>>(model["BaseParameters"].ToString());
                    //CollectParameters = System.Text.Json.JsonSerializer.Deserialize<Dictionary<MachineInfoTypes, ObservableCollection<CollectParameterModel>>>(model["CollectParameters"].ToString());

                    //MesOutputParameters = System.Text.Json.JsonSerializer.Deserialize<Dictionary<MachineInfoTypes, ObservableCollection<MesOutputParameterModel>>>(model["MesOutputParameters"].ToString(), GlobalStatic.Options);
                    //MesRunParameters = System.Text.Json.JsonSerializer.Deserialize<Dictionary<MyTuple, ObservableCollection<MesRunParameterModel>>>(model["MesRunParameters"].ToString(), GlobalStatic.Options);

                    var model = JsonConvert.DeserializeObject<Dictionary<string, object>>(File.ReadAllText(GlobalStatic.DeviceInteractiveaddressPath, System.Text.Encoding.UTF8));
                    BaseParameters = JsonConvert.DeserializeObject<Dictionary<MachineInfoTypes, ObservableCollection<BaseParameteModel>>>(model["BaseParameters"].ToString());
                    CollectParameters = JsonConvert.DeserializeObject<Dictionary<MachineInfoTypes, ObservableCollection<CollectParameterModel>>>(model["CollectParameters"].ToString());

                    MesOutputParameters = JsonConvert.DeserializeObject<Dictionary<MachineInfoTypes, ObservableCollection<MesOutputParameterModel>>>(model["MesOutputParameters"].ToString());


                    var settings = new JsonSerializerSettings();
                    settings.Converters.Add(new TupleJsonConverter());

                    MesRunParameters = JsonConvert.DeserializeObject<Dictionary<(MachineInfoTypes, ShiftTypes), ObservableCollection<MesRunParameterModel>>>(model["MesRunParameters"].ToString(), settings);

                    //MesRunParameters = JsonConvert.DeserializeObject<Dictionary<(MachineInfoTypes, ShiftTypes), ObservableCollection<MesRunParameterModel>>>(model["MesRunParameters"].ToString());

                    //MesRunParameters = newDict;
                }
                catch (Exception ex)
                {
                    //BaseParameters = new Dictionary<MachineInfoTypes, ObservableCollection<BaseParameteModel>>();
                    //CollectParameters= new Dictionary<MachineInfoTypes, ObservableCollection<CollectParameterModel>>();

                    //MesOutputParameters = new Dictionary<MachineInfoTypes, ObservableCollection<MesOutputParameterModel>>();
                    MesRunParameters = new Dictionary<(MachineInfoTypes, ShiftTypes), ObservableCollection<MesRunParameterModel>>(); 
                     
                    MessageBox.Show($"注意==>>触摸屏交互地址初始化异常：{ex}");
                }
            }

        }
        //public void Save() => System.IO.File.WriteAllText(GlobalStatic.DeviceInteractiveaddressPath, System.Text.Json.JsonSerializer.Serialize(this), Encoding.UTF8);
        public void Save() => System.IO.File.WriteAllText(GlobalStatic.DeviceInteractiveaddressPath, Newtonsoft.Json.JsonConvert.SerializeObject(this), Encoding.UTF8);
    }
}
